<?php

namespace StudyBuddy;

use \StudyBuddy\Forms\Form;

/**
 * Generates html of the QuickRegistration block
 * it extends RegBlock
 * the main difference is that this class also inserts
 * the Captcha into form (2 hidden fields, one text field and
 * an image)
 */
class RegBlockQuickReg extends RegBlock {

    protected $aUsername = array(
        'usernameLabel' => 'Choose a Username',
        'usernameVal' => '',
        'usernameNote' => 'Username will appear alongside your posts');

    protected function setUsernameVars() {
        return $this;
    }

    protected function prepareVars() {

        $this->aVars = array(
            'titleBar' => 'Welcome to ' . $this->oRegistry->Ini->SITE_NAME,
            'token' => Form::generateToken(), /* $this->oGlobal->addFormToken(), */
            'title' => 'Welcome to ' . $this->oRegistry->Ini->SITE_NAME,
            'header2' => '<div class="step2">Join us! It\'s super easy and free!</div>',
            'captcha' => $this->makeCaptchaBlock(),
            'action' => 'quickreg',
            'td1width' => '200px',
            'td2' => $this->makeSocialAuthBlock(),
            'loginBlock' => $this->makeLoginBlock()
        );

        $this->addUsernameBlock();

        return $this;
    }

    /**
     *
     * Add a small login block to the template
     * but ONLY for ajax based request
     *
     * for regular web page we don't need
     * to have yet another login block
     * in the registration block
     *
     * @return string html of login block
     * of empty string for non-ajax request
     */
    protected function makeLoginBlock() {

        if (Request::isAjax()) {
            return \tplLoginblock::parse(array());
        }

        return '';
    }

    /**
     * Makes html for the captcha block, complete
     * with image, input element for captcha
     * and 2 hidden fields
     *
     * @return string HTML block
     */
    protected function makeCaptchaBlock() {
        $s = Captcha::factory($this->oRegistry)->getCaptchaBlock();

        return $s;
    }

    /**
     * Make html block for the external
     * auth providers icons with links
     * 
     * @todo add LinkedIn button!
     *
     * @return string html
     */
    public function makeSocialAuthBlock($or = '<h2>-OR-</h2>') {
        $s = '';
        $oIni = $this->oRegistry->Ini;
        $GfcSiteID = $oIni->GFC_ID;

        if (isset($oIni->TWITTER)) {
            $aTW = $oIni['TWITTER'];
            if (!empty($aTW['TWITTER_OAUTH_KEY']) && !empty($aTW['TWITTER_OAUTH_SECRET'])) {
                //onClick="oSL.Twitter.startDance(); return false"
                $s .= '<div class="extauth"><a href="#" class="ajax twsignin"><img class="hand" src="/images/signin.png" width="151" height="24" alt="Sign in with Twitter account"/></a></div>';
            }
        }

        d('cp');
        if (isset($oIni->FACEBOOK)) {
            $aFB = $oIni['FACEBOOK'];
            d('cp');
            if (!empty($aFB['APP_ID'])) {
                d('cp');
                //onClick="oSL.initFBSignup(); return false"
                $s .= '<div class="extauth"><a href="#" class="ajax fbsignup"><img class="hand" src="/images/fblogin.png" width="154" height="22" alt="Sign in with Facebook account"/></a></div>';
            }
        }

        if (!empty($GfcSiteID)) {
            $s .= '<div class="extauth"><a href="#" onClick="google.friendconnect.requestSignIn(); return false;"><img class="hand" src="/images/gfcbutton.jpg" width="226" height="40" alt="Sign in with Google Friend Connect"/></a></div>';
        }

        $label = (!empty($s)) ? '<h3>Join with account you already have</h3><hr class="line1"/>' : '';

        return \tplSocial::parse(array($s, '', $label), false);
    }

}
